package demoMod.stsconsole.patches;

import demoMod.stsconsole.annotations.ByRef;
import demoMod.stsconsole.annotations.SpirePatch;
import demoMod.stsconsole.exceptions.PatchingException;
import javassist.CtBehavior;
import javassist.CtMethod;

public abstract class PatchInfo {
    private static int modNum = 0;
    private final int modOrder;
    SpirePatch patch;
    CtBehavior ctMethodToPatch;
    CtMethod patchMethod;

    public PatchInfo(CtBehavior ctMethodToPatch, CtMethod patchMethod) {
        this.ctMethodToPatch = ctMethodToPatch;
        this.patchMethod = patchMethod;
        this.modOrder = modNum;
    }

    public PatchInfo setSpirePatch(SpirePatch patch) {
        this.patch = patch;
        return this;
    }

    public void debugPrint() {
        System.out.println("Patch Class: [" + patchClassName() + "]");
        System.out.println(" - Patching [" + ctMethodToPatch.getLongName() + "]");
        System.out.print(" - ");
        System.out.println(debugMsg());
    }

    protected String patchClassName() {
        return patchMethod.getDeclaringClass().getName();
    }

    protected abstract String debugMsg();

    public abstract int patchOrdering();

    public abstract void doPatch() throws PatchingException;

    protected static boolean paramByRef(Object[] annotations) {
        for (Object o : annotations) {
            if (o instanceof ByRef) {
                return true;
            }
        }
        return false;
    }

    public static void nextMod() {
        ++modNum;
    }

    final public int modOrdering() {
        return modOrder;
    }
}
